home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / scm / slib / getparam.scm < prev    next >
Text File  |  1999-04-19  |  5KB  |  153 lines

  1. ;;; "getparam.scm" convert getopt to passing parameters by name.
  2. ; Copyright 1995, 1996, 1997 Aubrey Jaffer
  3. ;
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7. ;
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10. ;
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14. ;
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. (require 'getopt)
  21.  
  22. (define (getopt->parameter-list argc argv optnames arities types aliases)
  23.   (define (can-take-arg? opt)
  24.     (not (eq? (list-ref arities (position opt optnames))
  25.           'boolean)))
  26.   (define (coerce-val val curopt)
  27.     (define ntyp (list-ref types (position curopt optnames)))
  28.     (case ntyp
  29.       ((expression) val)
  30.       (else (coerce val ntyp))))
  31.   (let ((starting-optind *optind*)
  32.     (optlist '())
  33.     (long-opt-list '())
  34.     (optstring #f)
  35.     (parameter-list (make-parameter-list optnames))
  36.     (curopt '*unclaimed-argument*))
  37.     (set! aliases (map (lambda (alias)
  38.              (define str (string-copy (car alias)))
  39.              (do ((i (+ -1 (string-length str)) (+ -1 i)))
  40.                  ((negative? i) (cons str (cdr alias)))
  41.                (cond ((char=? #\ (string-ref str i))
  42.                   (string-set! str i #\-)))))
  43.                aliases))
  44.     (for-each
  45.      (lambda (alias)
  46.        (define opt (car alias))
  47.        (cond ((not (string? opt)))
  48.          ((< 1 (string-length opt))
  49.           (set! long-opt-list (cons opt long-opt-list)))
  50.          ((not (= 1 (string-length opt))))
  51.          ((can-take-arg? (cadr alias))
  52.           (set! optlist (cons (string-ref opt 0)
  53.                   (cons #\: optlist))))
  54.          (else (set! optlist (cons (string-ref opt 0) optlist)))))
  55.      aliases)
  56.     (set! optstring (list->string (cons #\: optlist)))
  57.     (let loop ()
  58.       (let ((opt (getopt-- argc argv optstring)))
  59.     (case opt
  60.       ((#\: #\?)
  61.        (parameter-list->getopt-usage (list-ref argv (+ -1 starting-optind))
  62.                      optnames arities types aliases)
  63.        (slib:error 'getopt->parameter-list
  64.                (case opt
  65.              ((#\:) "argument missing after")
  66.              ((#\?) "unrecognized option"))
  67.                (string #\- getopt:opt)))
  68.       ((#f)
  69.        (cond ((and (< *optind* argc)
  70.                (string=? "-" (list-ref argv *optind*)))
  71.           (set! *optind* (+ 1 *optind*)))
  72.          ((< *optind* argc)
  73.           (cond ((and (member curopt optnames)
  74.                   (adjoin-parameters!
  75.                    parameter-list
  76.                    (list curopt
  77.                      (coerce-val (list-ref argv *optind*)
  78.                          curopt))))
  79.              (set! *optind* (+ 1 *optind*))
  80.              (loop))
  81.             (else (slib:error 'getopt->parameter-list curopt
  82.                       (list-ref argv *optind*)
  83.                       "not supported"))))))
  84.       (else
  85.        (cond ((char? opt) (set! opt (string opt))))
  86.        (let ((topt (assoc opt aliases)))
  87.          (cond (topt (set! topt (cadr topt)))
  88.            (else (slib:error "Option not recognized -" opt)))
  89.          (cond
  90.           ((not (can-take-arg? topt))
  91.            (adjoin-parameters! parameter-list (list topt #t)))
  92.           (*optarg*
  93.            (set! curopt topt)
  94.            (adjoin-parameters! parameter-list
  95.                    (list topt (coerce-val *optarg* curopt))))
  96.           (else
  97.            (set! curopt topt)
  98. ;;;           (slib:warn 'getopt->parameter-list
  99. ;;;              "= missing for option--" opt)
  100.            )))
  101.        (loop)))))
  102.     parameter-list))
  103.  
  104. (define (parameter-list->getopt-usage comname optnames arities types aliases)
  105.   (require 'printf)
  106.   (require 'common-list-functions)
  107.   (let ((aliast (map list optnames))
  108.     (strlen=1? (lambda (s) (= 1 (string-length s))))
  109.     (cep (current-error-port)))
  110.     (for-each (lambda (alias)
  111.         (let ((apr (assq (cadr alias) aliast)))
  112.           (set-cdr! apr (cons (car alias) (cdr apr)))))
  113.           aliases)
  114.     (fprintf cep "Usage: %s [OPTION ARGUMENT ...] ..." comname)
  115.     (newline cep) (newline cep)
  116.     (for-each
  117.      (lambda (optname arity aliat)
  118.        (let loop ((initials (remove-if-not strlen=1? (cdr aliat)))
  119.           (longname (remove-if strlen=1? (cdr aliat))))
  120.      (cond ((and (null? initials) (null? longname)))
  121.            (else (fprintf cep
  122.                   (case arity
  123.                 ((boolean) "  %3s %s")
  124.                 (else "  %3s %s<%s> %s"))
  125.                   (if (null? initials)
  126.                   ""
  127.                   (string-append "-" (car initials)
  128.                          (if (null? longname) " " ",")))
  129.                   (if (null? longname)
  130.                   "      "
  131.                   (string-append "--" (car longname)
  132.                          (case arity
  133.                            ((boolean) " ")
  134.                            (else "="))))
  135.                   (case arity
  136.                 ((boolean) "")
  137.                 (else optname))
  138.                   (case arity
  139.                 ((nary nary1) "...")
  140.                 (else "")))
  141.              (newline cep)
  142.              (loop (if (null? initials) '() (cdr initials))
  143.                (if (null? longname) '() (cdr longname)))))))
  144.      optnames arities aliast)))
  145.  
  146. (define (getopt->arglist argc argv optnames positions
  147.              arities types defaulters checks aliases)
  148.   (let* ((params (getopt->parameter-list
  149.           argc argv optnames arities types aliases))
  150.      (fparams (fill-empty-parameters defaulters params)))
  151.     (and (list? params) (check-parameters checks fparams))
  152.     (and (list? params) (parameter-list->arglist positions arities fparams))))
  153.